package cn.enjoy.dynamic.datasource.config;

import cn.enjoy.dynamic.datasource.prop.DataSourceProperties;
import cn.enjoy.dynamic.datasource.prop.DynamicDataSourceProperties;
import cn.enjoy.dynamic.datasource.utils.DataSourceUtil;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置多数据源
 *
 * @author K
 * @date 2020/12/14 16:08
 */
@Configuration
@EnableConfigurationProperties(DynamicDataSourceProperties.class)
public class DynamicDataSourceConfig {

    /**
     * 注入动态数据源配置
     */
    @Autowired
    private DynamicDataSourceProperties properties;


    /**
     * 注入yml spring.datasource.druid 中默认数据源配置
     *
     * @return DataSourceProperties
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * 初始化注入所有数据源
     * 如果使用了baomidou的动态数据源，此处的注入需要注释掉
     *
     * @param dataSourceProperties 默认数据源配置
     * @return DynamicDataSource
     */
    @Bean
    public DynamicDataSource dynamicDataSource(DataSourceProperties dataSourceProperties) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        DataSourceUtil.addDataSource("默认", dataSourceProperties);
///
//        DruidDataSource defaultDataSource = DynamicDataSourceFactory.buildDruidDataSource(dataSourceProperties);
//        dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);
        // 动态数据源
        DataSourceUtil.addDataSourceBatch(properties.getDatasource());
        dynamicDataSource.setTargetDataSources(DataSourceUtil.dataSourceMap);

        return dynamicDataSource;
    }

    /**
     * 注册一个StatViewServlet, 进行druid监控页面配置
     *
     * @return servlet registration bean
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        //先配置管理后台的servLet，访问的入口为/druid/
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名单 (没有配置或者为空，则允许所有访问)
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        // IP黑名单 (存在共同时，deny优先于allow)
        servletRegistrationBean.addInitParameter("deny", "");
        servletRegistrationBean.addInitParameter("loginUsername", "druid");
        servletRegistrationBean.addInitParameter("loginPassword", "druid");
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * 注册一个过滤器，允许页面正常浏览
     *
     * @return filter registration bean
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}